package com.mgee.netty.server.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.protobuf.Any;
import com.mgee.netty.server.constant.MessagingConst;
import com.mgee.netty.server.netty.IoSession;
import com.mgee.netty.server.netty.SessionManager;
import com.mgee.netty.server.pojo.ChatUserInfo;
import com.mgee.netty.server.pojo.LobbyInfo;
import com.mgee.netty.server.pojo.LobbyRoomInfo;
import com.mgee.netty.server.protobuf.ImProto.AddPlayer;
import com.mgee.netty.server.protobuf.ImProto.CreatePlayerRequest;
import com.mgee.netty.server.protobuf.ImProto.CreatePlayerResponse;
import com.mgee.netty.server.protobuf.ImProto.ImMsg;
import com.mgee.netty.server.protobuf.ImProto.PlayerPack;
import com.mgee.netty.server.util.MessageBuilder;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author mileswan
 *
 */
@Service
@Slf4j
public class PlayerService {
	
	@Autowired
	private SessionManager sessionManager;
	
	public void createPlayer(ImMsg imMsg, IoSession currSession) {
		
		try {
			if(currSession.getUser() == null) {
				currSession.sendMessage(MessageBuilder.buildErrorResponse(imMsg, MessagingConst.RESPONSE_ERR_CODE_400, "用户未登录!"));
				return;
			}
			String senderUid = currSession.getUser().getUid();
			
			LobbyRoomInfo lobbyRoomInfo = SessionManager.userId2LobbyRooms.get(senderUid);
			if(lobbyRoomInfo == null) {
				currSession.sendMessage(MessageBuilder.buildErrorResponse(imMsg, MessagingConst.RESPONSE_ERR_CODE_400, "用户不在任何演练房间!"));
				return;
			}
			
			CreatePlayerRequest createPlayerRequest = imMsg.getMsg().unpack(CreatePlayerRequest.class);
			
			if(createPlayerRequest.getPlayerPack() != null) {
				PlayerPack.Builder playerPackBuilder = PlayerPack.newBuilder();
				playerPackBuilder.setUid(senderUid);
				playerPackBuilder.setNickName(createPlayerRequest.getPlayerPack().getNickName());
				playerPackBuilder.setChatno(createPlayerRequest.getPlayerPack().getChatno());
				playerPackBuilder.setRoleInfo(createPlayerRequest.getPlayerPack().getRoleInfo());
				
				currSession.getUser().setLobbyRoomPlayerPack(playerPackBuilder.build());
			}else {
				currSession.sendMessage(MessageBuilder.buildErrorResponse(imMsg, MessagingConst.RESPONSE_ERR_CODE_400, "人物信息不能为空!"));
				return;
			}
			
			//广播addPlayer消息
			LobbyInfo lobbyInfo = SessionManager.userId2Lobbies.get(senderUid);
			if(lobbyInfo != null) {
				AddPlayer.Builder addPlayerBuilder = AddPlayer.newBuilder();
				addPlayerBuilder.setPlayerPack(currSession.getUser().getLobbyRoomPlayerPack());
				ImMsg.Builder imMsgAddPlayerBuilder = ImMsg.newBuilder();
	            
				imMsgAddPlayerBuilder.setMsg(Any.pack(addPlayerBuilder.build()));
				imMsgAddPlayerBuilder.setId(imMsg.getId());
				//sessionManager.sendMessageToRoomUsers(imMsgAddPlayerBuilder.build(), senderUid);
				
				sessionManager.sendMessageToLobbyRoomPlayers(imMsgAddPlayerBuilder.build(), senderUid, false);
			}
			
			
	    	// 按照定义的数据结构，创建返回消息对象  
			CreatePlayerResponse.Builder createPlayerRspBuilder = CreatePlayerResponse.newBuilder();
    		ImMsg.Builder imMsgBuilder = ImMsg.newBuilder();
            //检查漫游房间
//    		String room_id = SessionManager.userId2GroupIds.get(senderUid);
//    		if(room_id == null || SessionManager.groupId2Users.get(room_id) == null) {
//    			//获取演练房间
//    			room_id = SessionManager.userId2LobbyRooms.get(senderUid).getName();
//    		}
    		
    		String room_id = lobbyRoomInfo.getName();
    		for(ChatUserInfo roomUser : SessionManager.lobbyRoomId2Users.get(room_id).values()) {
				if(roomUser.getLobbyRoomPlayerPack() == null) continue;
				createPlayerRspBuilder.addPlayerPack(roomUser.getLobbyRoomPlayerPack());
			}
            
            imMsgBuilder.setMsg(Any.pack(createPlayerRspBuilder.build()));
            imMsgBuilder.setId(imMsg.getId());
	        //发送反馈消息
	        currSession.sendMessage(imMsgBuilder.build());
	        
		}catch(Exception e) {
			log.error("创建人物时发生异常, ", e);
		}
		
		
	}
	
	
}
